/**
 * Created by aaronjin on 16/11/24.
 */

const webpack = require('webpack');
const autoprefixer = require('autoprefixer-core');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const resolve = require('path').resolve;

const extractSASS = new ExtractTextPlugin({
    filename: '[name].css',
    allChunks: true
});
const extractLESS = new ExtractTextPlugin({
    filename: 'lib.css',
    allChunks: true
});
module.exports = function (env) {

    const prodEnabled = env && env.prod;
    return {
        devtool: prodEnabled ? 'source-map' : 'eval',
        resolve: {
            modules: [
                'node_modules',
                resolve(__dirname, 'src'),
            ],
            extensions: ['.js', '.css', '.html', '.json'],

        },
        entry: {
            wap: [
                "webpack-dev-server/client?http://localhost:8080",
                "webpack/hot/only-dev-server",
                '../Cgi-Node-Commercial/client'
            ],
        },
        output: {
            path: resolve(__dirname, '../dist'),
            filename: '[name].js',
            publicPath: 'http://localhost:8080/',
        },
        plugins: [
            new webpack.HotModuleReplacementPlugin(),
            new webpack.NoErrorsPlugin(),
            extractSASS,
            extractLESS,

            new webpack.optimize.CommonsChunkPlugin({
                name:'vendor',
                filename:'vendor.js',
                minChunks:function(module,count){
                    var resourceName = module.resource;
                    var reg = /node_modules/;
                    if(reg.test(resourceName)){
                        return true;
                    }
                    return false;
                }
            }),

            new HtmlWebpackPlugin({
                template: './src/index.html',
                //favicon: './src/favicon.ico',
                filename: 'index.html',
            }),

        ],
        module: {
            loaders: [
                {
                    test: /\.(js|jsx)?$/,
                    exclude: /node_modules/,
                    loaders: ['babel-loader']
                },{
                    test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot|ico)$/,
                    loader: 'file-loader?name=img/[name].[ext]'
                },{
                    test: /\.html$/,
                    loader: 'html-loader'
                },{
                    test: /\.css$/,
                    loader: ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css?sourceMap!postcss' })
                },
                {
                    test: /\.scss$/,
                    loader: extractSASS.extract({ fallbackLoader: 'style-loader', loader: [
                        "css-loader",
                        "autoprefixer-loader?browsers=last 20 version",
                        "sass-loader?outputStyle=expanded"
                    ].join("!") })
                },
                {
                    test: /\.less$/,
                    loader: extractLESS.extract({ fallbackLoader: 'style-loader', loader: [
                        "css-loader",
                        "autoprefixer-loader?browsers=last 20 version",
                        "less-loader"
                    ].join("!") })
                }
            ],
        },
        devServer: {
            inline:true,
            port: 8080,
            contentBase: 'dist/',
            noInfo: true,
            historyApiFallback: true,
            hot:true,
            //proxy: {
            //  '/address/*': {
            //    target: 'http://webapp.ejiazheng.loc/',
            //  },
            //},
        },
    };

};